#!/bin/bash

software_dir="Whisper"
venv_name="amdgpu"
RUN_SCRIPT="run_${software_dir}.sh"

create_run_script() {
    echo "建立启动脚本"
    if [ ! -f "$RUN_SCRIPT" ]; then
        cat > "$RUN_SCRIPT" <<EOF
#!/bin/bash
source "$venv_name/bin/activate"
export HSA_OVERRIDE_GFX_VERSION=11.0.0
python app.py
EOF
        chmod +x "$RUN_SCRIPT"
    fi
}

sudo apt-get install ffmpeg -y
#下载文件
[ ! -d "$software_dir" ] && git clone https://github.com/jhj0517/Whisper-WebUI.git $software_dir
cd $software_dir
[ ! -d "$venv_name" ] && python3 -m venv $venv_name
source $venv_name/bin/activate

#安装环境
pip3 install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/rocm6.0
sed -i '/torch/d' requirements.txt
sed -i '/--extra-index-url https://download.pytorch.org/whl/cu121/d' requirements.txt
pip3 install -r requirements.txt

cd modules
sed -i 's/self.available_compute_types = ctranslate2.get_supported_compute_types("cuda") if self.device == "cuda" else ctranslate2.get_supported_compute_types("cpu")/self.available_compute_types = ['default']/g' faster_whisper_inference.py
sed -i 's/self.current_compute_type = "float16" if self.device == "cuda" else "float32"/self.current_compute_type = 'default'/g' faster_whisper_inference.py

cd ..

create_run_script


echo "安装完成"
